// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyavtVarMetaData.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>
#include <avtTypes.h>

// ****************************************************************************
// Module: PyavtVarMetaData
//
// Purpose:
//   Contains metadata attributes associated with all mesh variables
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a avtVarMetaData.
//
struct avtVarMetaDataObject
{
    PyObject_HEAD
    avtVarMetaData *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewavtVarMetaData(int);
std::string
PyavtVarMetaData_ToString(const avtVarMetaData *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    str = PyavtBaseVarMetaData_ToString(atts, prefix, forLogging);

    const char *centering_names = "AVT_NODECENT, AVT_ZONECENT, AVT_NO_VARIABLE, AVT_UNKNOWN_CENT";
    if(atts->centering == AVT_NODECENT)
    {
        snprintf(tmpStr, 1000, "%scentering = %sAVT_NODECENT  # %s\n", prefix, prefix, centering_names);
        str += tmpStr;
    }
    else if(atts->centering == AVT_ZONECENT)
    {
        snprintf(tmpStr, 1000, "%scentering = %sAVT_ZONECENT  # %s\n", prefix, prefix, centering_names);
        str += tmpStr;
    }
    else if(atts->centering == AVT_NO_VARIABLE)
    {
        snprintf(tmpStr, 1000, "%scentering = %sAVT_NO_VARIABLE  # %s\n", prefix, prefix, centering_names);
        str += tmpStr;
    }
    else
    {
        snprintf(tmpStr, 1000, "%scentering = %sAVT_UNKNOWN_CENT  # %s\n", prefix, prefix, centering_names);
        str += tmpStr;
    }

    if(atts->hasUnits)
        snprintf(tmpStr, 1000, "%shasUnits = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%shasUnits = 0\n", prefix);
    str += tmpStr;
    snprintf(tmpStr, 1000, "%sunits = \"%s\"\n", prefix, atts->units.c_str());
    str += tmpStr;
    if(atts->hasDataExtents)
        snprintf(tmpStr, 1000, "%shasDataExtents = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%shasDataExtents = 0\n", prefix);
    str += tmpStr;
    snprintf(tmpStr, 1000, "%sminDataExtents = %g\n", prefix, atts->minDataExtents);
    str += tmpStr;
    snprintf(tmpStr, 1000, "%smaxDataExtents = %g\n", prefix, atts->maxDataExtents);
    str += tmpStr;
    {   const intVector &matRestricted = atts->matRestricted;
        snprintf(tmpStr, 1000, "%smatRestricted = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < matRestricted.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%d", matRestricted[i]);
            str += tmpStr;
            if(i < matRestricted.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    return str;
}

static PyObject *
avtVarMetaData_Notify(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
avtVarMetaData_SetCentering(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;

    int ival = -999;
    if (PySequence_Check(args) && !PyArg_ParseTuple(args, "i", &ival))
        return PyErr_Format(PyExc_TypeError, "Expecting scalar integer arg");
    else if (PyNumber_Check(args) && (ival = PyLong_AsLong(args)) == -1 && PyErr_Occurred())
        return PyErr_Format(PyExc_TypeError, "Expecting scalar integer arg");
    if (ival == -999)
        return PyErr_Format(PyExc_TypeError, "Expecting scalar integer arg");

    obj->data->centering = (avtCentering)ival;

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
avtVarMetaData_GetCentering(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->centering));
    return retval;
}

/*static*/ PyObject *
avtVarMetaData_SetHasUnits(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the hasUnits in the object.
    obj->data->hasUnits = cval;

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
avtVarMetaData_GetHasUnits(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->hasUnits?1L:0L);
    return retval;
}

/*static*/ PyObject *
avtVarMetaData_SetUnits(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the units in the object.
    obj->data->units = cval;

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
avtVarMetaData_GetUnits(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;
    PyObject *retval = PyString_FromString(obj->data->units.c_str());
    return retval;
}

/*static*/ PyObject *
avtVarMetaData_SetHasDataExtents(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the hasDataExtents in the object.
    obj->data->hasDataExtents = cval;

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
avtVarMetaData_GetHasDataExtents(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->hasDataExtents?1L:0L);
    return retval;
}

/*static*/ PyObject *
avtVarMetaData_SetMinDataExtents(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    double val = PyFloat_AsDouble(args);
    double cval = double(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ double");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ double");
    }

    Py_XDECREF(packaged_args);

    // Set the minDataExtents in the object.
    obj->data->minDataExtents = cval;

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
avtVarMetaData_GetMinDataExtents(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;
    PyObject *retval = PyFloat_FromDouble(obj->data->minDataExtents);
    return retval;
}

/*static*/ PyObject *
avtVarMetaData_SetMaxDataExtents(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    double val = PyFloat_AsDouble(args);
    double cval = double(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ double");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ double");
    }

    Py_XDECREF(packaged_args);

    // Set the maxDataExtents in the object.
    obj->data->maxDataExtents = cval;

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
avtVarMetaData_GetMaxDataExtents(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;
    PyObject *retval = PyFloat_FromDouble(obj->data->maxDataExtents);
    return retval;
}

/*static*/ PyObject *
avtVarMetaData_SetMatRestricted(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;

    intVector vec;

    if (PyNumber_Check(args))
    {
        long val = PyLong_AsLong(args);
        int cval = int(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ int");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ int");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            long val = PyLong_AsLong(item);
            int cval = int(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ int", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ int", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more ints");

    obj->data->matRestricted = vec;
    // Mark the matRestricted in the object as modified.
    obj->data->SelectAll();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
avtVarMetaData_GetMatRestricted(PyObject *self, PyObject *args)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)self;
    // Allocate a tuple the with enough entries to hold the matRestricted.
    const intVector &matRestricted = obj->data->matRestricted;
    PyObject *retval = PyTuple_New(matRestricted.size());
    for(size_t i = 0; i < matRestricted.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyInt_FromLong(long(matRestricted[i])));
    return retval;
}



PyMethodDef PyavtVarMetaData_methods[AVTVARMETADATA_NMETH] = {
    {"Notify", avtVarMetaData_Notify, METH_VARARGS},
    {"SetCentering", avtVarMetaData_SetCentering, METH_VARARGS},
    {"GetCentering", avtVarMetaData_GetCentering, METH_VARARGS},
    {"SetHasUnits", avtVarMetaData_SetHasUnits, METH_VARARGS},
    {"GetHasUnits", avtVarMetaData_GetHasUnits, METH_VARARGS},
    {"SetUnits", avtVarMetaData_SetUnits, METH_VARARGS},
    {"GetUnits", avtVarMetaData_GetUnits, METH_VARARGS},
    {"SetHasDataExtents", avtVarMetaData_SetHasDataExtents, METH_VARARGS},
    {"GetHasDataExtents", avtVarMetaData_GetHasDataExtents, METH_VARARGS},
    {"SetMinDataExtents", avtVarMetaData_SetMinDataExtents, METH_VARARGS},
    {"GetMinDataExtents", avtVarMetaData_GetMinDataExtents, METH_VARARGS},
    {"SetMaxDataExtents", avtVarMetaData_SetMaxDataExtents, METH_VARARGS},
    {"GetMaxDataExtents", avtVarMetaData_GetMaxDataExtents, METH_VARARGS},
    {"SetMatRestricted", avtVarMetaData_SetMatRestricted, METH_VARARGS},
    {"GetMatRestricted", avtVarMetaData_GetMatRestricted, METH_VARARGS},
    {NULL, NULL}
};

static void PyavtVarMetaData_ExtendSetGetMethodTable()
{
    static bool extended = false;
    if (extended) return;
    extended = true;

    int i = 0;
    while (PyavtVarMetaData_methods[i].ml_name)
        i++;
    int n = i;
    while (PyavtBaseVarMetaData_methods[i-n+1].ml_name)
    {
        PyavtVarMetaData_methods[i] = PyavtBaseVarMetaData_methods[i-n+1];
        i++;
    }

    PyMethodDef nullMethod = {NULL, NULL};
    PyavtVarMetaData_methods[i] = nullMethod;
}

//
// Type functions
//

static void
avtVarMetaData_dealloc(PyObject *v)
{
   avtVarMetaDataObject *obj = (avtVarMetaDataObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *avtVarMetaData_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyavtVarMetaData_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "centering") == 0)
        return avtVarMetaData_GetCentering(self, NULL);
    if(strcmp(name, "AVT_NODECENT") == 0)
        return PyInt_FromLong(long(AVT_NODECENT));
    else if(strcmp(name, "AVT_ZONECENT") == 0)
        return PyInt_FromLong(long(AVT_ZONECENT));
    else if(strcmp(name, "AVT_NO_VARIABLE") == 0)
        return PyInt_FromLong(long(AVT_NO_VARIABLE));
    else if(strcmp(name, "AVT_UNKNOWN_CENT") == 0)
        return PyInt_FromLong(long(AVT_UNKNOWN_CENT));

    if(strcmp(name, "hasUnits") == 0)
        return avtVarMetaData_GetHasUnits(self, NULL);
    if(strcmp(name, "units") == 0)
        return avtVarMetaData_GetUnits(self, NULL);
    if(strcmp(name, "hasDataExtents") == 0)
        return avtVarMetaData_GetHasDataExtents(self, NULL);
    if(strcmp(name, "minDataExtents") == 0)
        return avtVarMetaData_GetMinDataExtents(self, NULL);
    if(strcmp(name, "maxDataExtents") == 0)
        return avtVarMetaData_GetMaxDataExtents(self, NULL);
    if(strcmp(name, "matRestricted") == 0)
        return avtVarMetaData_GetMatRestricted(self, NULL);

    if(strcmp(name, "__methods__") != 0)
    {
        PyObject *retval = PyavtBaseVarMetaData_getattr(self, name);
        if (retval) return retval;
    }

    PyavtVarMetaData_ExtendSetGetMethodTable();

    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyavtVarMetaData_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyavtVarMetaData_methods[i].ml_name),
                PyString_FromString(PyavtVarMetaData_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyavtVarMetaData_methods, self, name);
}

int
PyavtVarMetaData_setattr(PyObject *self, char *name, PyObject *args)
{
    if (PyavtBaseVarMetaData_setattr(self, name, args) != -1)
        return 0;
    else
        PyErr_Clear();

    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "centering") == 0)
        obj = avtVarMetaData_SetCentering(self, args);
    else if(strcmp(name, "hasUnits") == 0)
        obj = avtVarMetaData_SetHasUnits(self, args);
    else if(strcmp(name, "units") == 0)
        obj = avtVarMetaData_SetUnits(self, args);
    else if(strcmp(name, "hasDataExtents") == 0)
        obj = avtVarMetaData_SetHasDataExtents(self, args);
    else if(strcmp(name, "minDataExtents") == 0)
        obj = avtVarMetaData_SetMinDataExtents(self, args);
    else if(strcmp(name, "maxDataExtents") == 0)
        obj = avtVarMetaData_SetMaxDataExtents(self, args);
    else if(strcmp(name, "matRestricted") == 0)
        obj = avtVarMetaData_SetMatRestricted(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
avtVarMetaData_print(PyObject *v, FILE *fp, int flags)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)v;
    fprintf(fp, "%s", PyavtVarMetaData_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
avtVarMetaData_str(PyObject *v)
{
    avtVarMetaDataObject *obj = (avtVarMetaDataObject *)v;
    return PyString_FromString(PyavtVarMetaData_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *avtVarMetaData_Purpose = "Contains metadata attributes associated with all mesh variables";
#else
static char *avtVarMetaData_Purpose = "Contains metadata attributes associated with all mesh variables";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(avtVarMetaDataType,         \
                  "avtVarMetaData",           \
                  avtVarMetaDataObject,       \
                  avtVarMetaData_dealloc,     \
                  avtVarMetaData_print,       \
                  PyavtVarMetaData_getattr,   \
                  PyavtVarMetaData_setattr,   \
                  avtVarMetaData_str,         \
                  avtVarMetaData_Purpose,     \
                  avtVarMetaData_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
avtVarMetaData_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &avtVarMetaDataType
         || Py_TYPE(other) != &avtVarMetaDataType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    avtVarMetaData *a = ((avtVarMetaDataObject *)self)->data;
    avtVarMetaData *b = ((avtVarMetaDataObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static avtVarMetaData *defaultAtts = 0;
static avtVarMetaData *currentAtts = 0;

static PyObject *
NewavtVarMetaData(int useCurrent)
{
    avtVarMetaDataObject *newObject;
    newObject = PyObject_NEW(avtVarMetaDataObject, &avtVarMetaDataType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new avtVarMetaData(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new avtVarMetaData(*defaultAtts);
    else
        newObject->data = new avtVarMetaData;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapavtVarMetaData(const avtVarMetaData *attr)
{
    avtVarMetaDataObject *newObject;
    newObject = PyObject_NEW(avtVarMetaDataObject, &avtVarMetaDataType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (avtVarMetaData *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
avtVarMetaData_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewavtVarMetaData(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef avtVarMetaDataMethods[] = {
    {"avtVarMetaData", avtVarMetaData_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *avtVarMetaDataObserver = 0;

std::string
PyavtVarMetaData_GetLogString()
{
    std::string s("avtVarMetaData = avtVarMetaData()\n");
    if(currentAtts != 0)
        s += PyavtVarMetaData_ToString(currentAtts, "avtVarMetaData.", true);
    return s;
}

static void
PyavtVarMetaData_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("avtVarMetaData = avtVarMetaData()\n");
        s += PyavtVarMetaData_ToString(currentAtts, "avtVarMetaData.", true);
        cb(s);
    }
}

void
PyavtVarMetaData_StartUp(avtVarMetaData *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyavtVarMetaData_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(avtVarMetaDataObserver == 0)
    {
        avtVarMetaDataObserver = new ObserverToCallback(subj,
            PyavtVarMetaData_CallLogRoutine, (void *)data);
    }

}

void
PyavtVarMetaData_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete avtVarMetaDataObserver;
    avtVarMetaDataObserver = 0;
}

PyMethodDef *
PyavtVarMetaData_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return avtVarMetaDataMethods;
}

bool
PyavtVarMetaData_Check(PyObject *obj)
{
    return (obj->ob_type == &avtVarMetaDataType);
}

avtVarMetaData *
PyavtVarMetaData_FromPyObject(PyObject *obj)
{
    avtVarMetaDataObject *obj2 = (avtVarMetaDataObject *)obj;
    return obj2->data;
}

PyObject *
PyavtVarMetaData_New()
{
    return NewavtVarMetaData(0);
}

PyObject *
PyavtVarMetaData_Wrap(const avtVarMetaData *attr)
{
    return WrapavtVarMetaData(attr);
}

void
PyavtVarMetaData_SetParent(PyObject *obj, PyObject *parent)
{
    avtVarMetaDataObject *obj2 = (avtVarMetaDataObject *)obj;
    obj2->parent = parent;
}

void
PyavtVarMetaData_SetDefaults(const avtVarMetaData *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new avtVarMetaData(*atts);
}

